$(function () {
    obj.init();
});
var obj = new JsFun();
function JsFun() {
    return {
        targetObj: null,
        applyForm: null,
        form: null,
        init: function () {
            this.applyForm = $('#xh_form');
            this.form = new xhForm(this.applyForm);
            this.funObj().getType();
            this.funObj().getPurpose();
            this.listener();
        },
        listener: function () {
            var $this = this;
            $('#sModel').unbind('change').on('change',function () {//修改类型
                //获取商品填入
                $this.funObj().getProd();
            });
            $('#iDestination').unbind('change').on('change',function () {//修改目的地
                $this.funObj().destinationChange();
            });
            $('#bt_modal_confirm',parent.document).click(function () {
                $this.funObj().saveCreate();
            });
        },
        funObj: function () {
            var $this = this;
            var form = this.applyForm;
            return {
                getType: function () {
                    xhExtra.ajaxRequest('device/getTabletData', {model:$('#sModel_data').val()}, {check:true}, function (data) {
                        $this.funObj().fillSelect('sModel',data.model,null,null,$('#sModel_data'));
                        if (data.Imei !== null) {
                            $('#sBrand').val(data.Imei);//接口返回是sBrand的值
                        }
                    });
                },
                getPurpose: function () {
                    xhExtra.ajaxRequest('device/getPurposeData', null, {check:true}, function (data) {
                        $this.funObj().fillSelect('purpose',data,null,null,$('#purpose_data'));
                        $this.funObj().addProduct();
                    });
                },
                fillSelect: function (selector,data,id,text,select) {
                    var str = '<option value="" style="display: none" selected>请选择</option>';
                    if(id != null && text != null){
                        $.each(data,function (key,val) {
                            str += '<option value="'+val[id]+'">'+val[text]+'</option>';
                        })
                    } else {
                        if (select !== null){
                            $.each(data,function (key,val) {
                                if (val === select.val()){
                                    str += '<option value="'+key+'" selected>'+val+'</option>';
                                }else{
                                    str += '<option value="'+key+'">'+val+'</option>';
                                }
                            })
                        } else {
                            $.each(data,function (key,val) {
                                str += '<option value="'+key+'">'+val+'</option>';
                            })
                        }
                    }
                    $('#'+selector).empty().append(str);
                },
                getProd: function () {
                    var sModel = $('#sModel').val();
                    xhExtra.ajaxRequest('device/getBrandData', {model:sModel}, {check:false}, function (data) {
                        if (data !== 'null'){
                            $("#sBrand").val(data);
                        } else {
                            $("#sBrand").val('');
                        }
                    });
                },
                destinationChange: function () {
                    var text = $('#iDestination').val();
                    var str ='';

                    var now = new Date();
                    var day = ("0" + now.getDate()).slice(-2);//格式化月，如果小于9，前面补0
                    var month = ("0" + (now.getMonth() + 1)).slice(-2);//拼装完整日期格式
                    var today = now.getFullYear()+"-"+(month)+"-"+(day);

                    if($this.targetObj != null){
                        $this.targetObj.remove();
                    }
                    if(text !== '1'){
                        str = '<tr>' +
                            '<td><label >快递公司:</label></td><td><input id="expressCompany" name="expressCompany" class="form-control" placeholder="如申通、顺丰等等，必填"/></td>' +
                            '<td><label >送修时间:</label></td><td><input type="date" id="repairDate" name="repairDate" class="form-control" value="'+today+'"/></td>' +
                            '</tr>';
                        str += '<tr>' +
                            '<td><label >快递单号:</label></td><td colspan="3"><input id="expressSend" name="expressSend" class="form-control" placeholder="必填"/></td>';
                        $this.targetObj = $(str);
                        $('#sure').remove();
                        form.find('table tbody').append($this.targetObj);
                    }
                },
                addProduct: function () {
                    var purpose_id = $('#purpose').val();
                    if (purpose_id === "6"){
                        xhExtra.ajaxRequest('device/getTabletsData', null, {check:false}, function (data) {
                            $this.funObj().fillSelect('product',data,null,null,null);
                        });
                    }
                },
                saveCreate: function () {
                    var userId = $('#id_data').val();
                    var logonName = $('#logonName').val();
                    var userName = $('#userName').val();
                    var sModel = $('#sModel').val();
                    var sImei = $('#IMei').val();
                    var schoolId = $('#schoolId_data').val();
                    var sBrand = $('#sBrand').val();
                    var purpose = $('#purpose').val();
                    var product = $('#product').val();
                    var remark = $('#remark').val();
                    if (sImei === null || sImei === ''){
                        window.top.xhTop.showError('IMei号不能为空');
                        xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                        return false;
                    }
                    if (sModel === null || sModel === ''){
                        window.top.xhTop.showError('型号不能为空');
                        xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                        return false;
                    }
                    if (sBrand === null || sBrand === ''){
                        window.top.xhTop.showError('品牌不能为空');
                        xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                        return false;
                    }
                    if (remark === null || remark === ''){
                        window.top.xhTop.showError('故障描述不能为空');
                        xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                        return false;
                    }
                    if (purpose === '6' && product === ''){
                        window.top.xhTop.showError('商品不能为空');
                        xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                        return false;
                    }
                    xhExtra.ajaxRequest('device/saveRepairData', {type:2, userId:userId, logonName:logonName, userName:userName,sImei:sImei,schoolId:schoolId,
                            sDeviceNumber:$('#sDeviceNumber').val(),sModel:sModel,sBrand:sBrand,remark:remark,
                            iDestination:$('#iDestination').val(),expressCompany:$('#expressCompany').val(),repairDate:$('#repairDate').val(),expressSend:$('#expressSend').val(),
                            purpose:purpose,product:product
                        },
                        {success: true,check:false},function (res) {
                            if(res.code === 200){
                                window.top.swal('成功',res.msg,'success');
                                window.top.xhTop.closeModal();
                            } else {
                                window.top.xhTop.showError(res.error);
                                xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                            }
                        });
                }
            }
        }
    }
}
